//
//  LLChannelShowView.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/2.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLChannelShowView.h"
#import "LLChannelReusableView.h"
#import "LLChannelShowCell.h"
#import "LLChannelModel.h"
#import "LLLaunchReusableView.h"

@interface LLChannelShowView ()<UICollectionViewDelegate, UICollectionViewDataSource, LLChannelReusableViewDelegate>
@property (strong, nonatomic) UILabel *titleLbl;
@property (strong, nonatomic) CALayer *separateLine;
@property (strong, nonatomic) LLButton *closeBtn;
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *layout;
@property (strong, nonatomic) NSMutableArray *jinengArr;
@property (strong, nonatomic) NSMutableArray *instatArray;



@end

@implementation LLChannelShowView

-(NSMutableArray *)jinengArr{
    if (!_jinengArr) {
        _jinengArr = [NSMutableArray array];
    }
    return _jinengArr;
}
-(NSMutableArray *)instatArray{
    if (!_instatArray) {
        _instatArray = [NSMutableArray array];
    }
    return _instatArray;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    _titleLbl = [LLInitBaseViewTool labelWithText:@"全部频道" textColor:LLColorRGB(0) font:0 textAlignment:NSTextAlignmentCenter];
    _titleLbl.frame = CGRectMake((self.width-150)/2.0, 30, 150, 30);
    _titleLbl.font = [UIFont boldSystemFontOfSize:18];
    [self addSubview:_titleLbl];
    
    _closeBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"close" titleColor:nil font:0];
    _closeBtn.frame = CGRectMake(self.width-30-10, 30, 30, 30);
    [_closeBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_closeBtn];
    
    _separateLine = CALayer.layer;
    _separateLine.frame = CGRectMake(0, 70, self.width, 1);
    _separateLine.backgroundColor = LLColorRGB(235).CGColor;
    [self.layer addSublayer:_separateLine];
    
    self.layout = UICollectionViewFlowLayout.new;
    self.layout.minimumLineSpacing = 15;
    self.layout.minimumInteritemSpacing = 10;
    self.layout.sectionInset = UIEdgeInsetsMake(0, 13, 0, 13);
    self.layout.itemSize = CGSizeMake((self.width-10*3-13*2)/4.0, 35);
    self.layout.headerReferenceSize = CGSizeMake(LLScreenWidth, 70);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, _separateLine.bottom, self.width, self.height-_separateLine.bottom) collectionViewLayout:self.layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = UIColor.whiteColor;
    [_collectionView registerClass:[LLChannelShowCell class] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerClass:[LLChannelReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LLChannelReusableView"];
        [_collectionView registerClass:[LLLaunchReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];//注册header的view
    [self addSubview:_collectionView];
}

- (void)setModels:(NSArray *)models {
    _models = models;
    for (LLChannelModel *model in models) {
        if (model.type.integerValue == 1) {
            [self.instatArray addObject:model];
        }else{
            [self.jinengArr addObject:model];
        }
    }
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDelegate & UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }else if (section == 1){
      return  self.instatArray.count;
    }else{
       return self.jinengArr.count;
    }
}
//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    return section == 0? self.models.count:self.jinengArr.count;
//}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
         self.layout.headerReferenceSize = CGSizeMake(LLScreenWidth, 70);
        LLChannelReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LLChannelReusableView" forIndexPath:indexPath];
        header.delegate = self;
        return header;
    }
     self.layout.headerReferenceSize = CGSizeMake(LLScreenWidth, 50);
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        for (UIView *view in headerView.subviews) {
            [view removeFromSuperview];
        }
    
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, LLScreenWidth, 50)];
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:18];
        label.textAlignment = NSTextAlignmentCenter;
        if (indexPath.section == 1) {
            label.text = @"选择你的兴趣";
        }else{
            label.text = @"选择你的技能";
        }
        [headerView addSubview:label];
        return headerView;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LLChannelShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 1) {
        cell.model = self.instatArray[indexPath.item];
    }else{
         cell.model = self.jinengArr[indexPath.item];
    }
    
    return cell;
}
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
//    for (UIView *view in headerView.subviews) {
//        [view removeFromSuperview];
//    }
//
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, LLScreenWidth, 100)];
//    label.textColor = [UIColor blackColor];
//    label.font = [UIFont systemFontOfSize:18];
//    label.textAlignment = NSTextAlignmentCenter;
//    if (indexPath.section == 0) {
//        label.text = @"选择你的兴趣";
//    }else{
//        label.text = @"选择你的技能";
//    }
//    [headerView addSubview:label];
//    return headerView;
//}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        LLChannelModel *model = self.instatArray[indexPath.item];
        if (model.isSelected.integerValue == 0) {
            model.isSelected = @"1";
        }else{
            model.isSelected = @"0";
        }
    }else if(indexPath.section == 2){
        LLChannelModel *model = self.jinengArr[indexPath.item];
        if (model.isSelected.integerValue == 0) {
            model.isSelected = @"1";
        }else{
            model.isSelected = @"0";
        }
    }

    
    [collectionView reloadData];
}

#pragma mark - LLChannelReusableViewDelegate

- (void)channelReusableViewDidClickEditBtn {
    NSMutableArray *array = [NSMutableArray array];
    NSMutableArray *array1 = [NSMutableArray array];
    for ( LLChannelModel *model  in self.instatArray) {
        if (model.isSelected.integerValue == 1) {
            [array addObject:model.interestId];
             [array1 addObject:model];
        }
    }
    for ( LLChannelModel *model  in self.jinengArr) {
        if (model.isSelected.integerValue == 1) {
            [array addObject:model.interestId];
            [array1 addObject:model];
        }
    }

    
    if (array.count == 0) {
        [self hide];
    }else{
        //发送请求
        [self sendTagRequest:array andModel:array1];
    }
}

-(void)sendTagRequest:(NSArray *)array andModel:(NSArray *)array1{
    NSMutableArray *array2 = [NSMutableArray array];
    for (LLChannelModel *model  in array1) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:model.interestId forKey:@"interest_id"];
         [dic setObject:model.isSelected forKey:@"isSelected"];
         [dic setObject:model.interest_name forKey:@"interest_name"];
         [dic setObject:model.type forKey:@"type"];
         [dic setObject:model.interestId forKey:@"interestId"];
        [array2 addObject:dic];
    }
    [LLUserDefaults setObject:array2 forKey:@"user_interest"];
    
    if (self.delegate) {
        [self.delegate updateView:array1];
    }
     [self hide];
    
//    LLUserInfo *user = [LLUserInfo sharedUser];
    
//    NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary: @{@"edu_id":@"33",@"user_random":user.user_random,@"user_id":user.user_id,@"interest":array}];
//    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
//
//        if ([result[@"code"] integerValue] == 0) {
//            [self showSVProgressHUDInfoWithsStatus:@"修改成功~"];
//            if (self.delegate) {
//                [self.delegate updateView:array1];
//            }
//
//            [self hide];
//        }
//
//    } failure:^(NSString *msg) {
//        [SVProgressHUD showErrorWithStatus:msg];
//
//    }];

}

#pragma mark - LLChannelShowCellDelegate

- (void)channelShowCellDidClickCell:(LLChannelShowCell *)ccell {
    [self hide];
//    LLChannelModel *model = ccell.model;
}

- (void)show {
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.top = 0;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide {
    [UIView animateWithDuration:0.3 animations:^{
        self.top = LLScreenHeight;
    } completion:^(BOOL finished) {
    }];
}



@end
